package com.guojiang.guoaicodemother.aop;


import cn.hutool.core.util.ObjectUtil;
import com.guojiang.guoaicodemother.annotation.AuthCheck;
import com.guojiang.guoaicodemother.exception.BusinessException;
import com.guojiang.guoaicodemother.exception.ErrorCode;
import com.guojiang.guoaicodemother.exception.ThrowUtils;
import com.guojiang.guoaicodemother.model.entity.User;
import com.guojiang.guoaicodemother.model.enums.UserRoleEnum;
import com.guojiang.guoaicodemother.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * 自定义权限校验切面逻辑
 */
@Aspect
@Component
public class AuthInterceptor {

    @Resource
    private UserService userService;

    /**
     * 执行拦截
     *
     * @param joinPoint
     * @param authCheck
     * @return
     */
    @Around("@annotation(authCheck)")
    public Object doInterceptor(ProceedingJoinPoint joinPoint, AuthCheck authCheck) throws Throwable {
        // 获取注解所描述的权限
        String mustRole = authCheck.mustRole();
        UserRoleEnum mustRoleEnum = UserRoleEnum.getEnumByValue(mustRole);
        // 如果要求的权限为空，直接放行
        if (ObjectUtil.isNull(mustRoleEnum)) return joinPoint.proceed();
        // 如果需要校验权限
        // 获得当前用户
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        HttpServletRequest httpServletRequest = ((ServletRequestAttributes) requestAttributes).getRequest();
        User loginUser = userService.getLoginUser(httpServletRequest);
        UserRoleEnum userRoleEnum = UserRoleEnum.getEnumByValue(loginUser.getUserRole());
        // 用户所拥有权限为空，直接拒绝，并提示无权限
        ThrowUtils.throwIf(ObjectUtil.isNull(userRoleEnum), new BusinessException(ErrorCode.NO_AUTH_ERROR));
        // 开始校验：
        switch (mustRoleEnum) {
            case ADMIN:
                if (!UserRoleEnum.ADMIN.equals(userRoleEnum)) {
                    throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "无权操作");
                }
                break;
            case SVIP:
                // 超级会员权限：需要是超级会员或管理员
                if (!UserRoleEnum.VIP.equals(userRoleEnum) && !UserRoleEnum.ADMIN.equals(userRoleEnum)) {
                    throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "无权操作");
                }
                break;
            case VIP:
                // 会员权限：需要是会员、超级会员或管理员
                if (!UserRoleEnum.SVIP.equals(userRoleEnum) &&
                        !UserRoleEnum.VIP.equals(userRoleEnum) &&
                        !UserRoleEnum.ADMIN.equals(userRoleEnum)) {
                    throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "无权操作");
                }
                break;
        }
        // 逻辑通过，放行
        return joinPoint.proceed();
    }
}
